<?php
/**
 * Created by PhpStorm.
 * User: chniccs
 * Date: 2019-12-05
 * Time: 11:03
 */

namespace app\app\controller;


use app\helper\OssHelper;
use app\util\ReturnCode;

class Upload extends Base
{
    /**
     * 通过服务器中转上传阿里云oss
     * @return array|string
     *
     */
    public function uploadImg()
    {
        $fid = $this->request->param('fid');
        $result = OssHelper::uploadFile($this->getUserTid());
        if (!$result['done']) {
            return $this->buildFailed(ReturnCode::INVALID, '上传失败', $result);
        } else {
            $data = ['name' => $result['name'], 'file_type' => $result['file_type'], 'file_name' => $result['file_name'], 'url' => $result['url'], 'fid' => $fid];
            return $this->buildSuccess($data, '上传成功');

        }
    }

    /**
     * 删除图片
     * @return array
     */
    public function delImg()
    {
        $name = $this->request->param('name');

        $result = OssHelper::deleteOssFile($name,$this->getUserTid());
        if ($result['done']) {
            return $this->buildSuccess($result, '删除成功');
        } else {
            return $this->buildFailed(ReturnCode::DELETE_FAILED, '删除失败', null);
        }

    }
}